﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TopNewLib.TopNews
{
    public class WeiBoTopClass:BaseTop
    {
        public WeiBoTopClass()
        {
            LinkURL = "http://s.weibo.com/top/summary?cate=realtimehot";
        }

        public WeiBoTopClass(string _linkUrl)
        {
            LinkURL = _linkUrl;
        }

        public override List<TopNewItem> GetCurTopNews()
        {
            List<TopNewItem> rs = new List<TopNewItem>();

            string htmlstr = GetHtmlInfo();

            int index1 = htmlstr.IndexOf("<table");
            int index2 = htmlstr.IndexOf("table>",index1);

            string subhtml = htmlstr.Substring(index1, index2 - index1 + "table>".Length);
            subhtml = System.Text.RegularExpressions.Regex.Unescape(subhtml);

            var doc = new HtmlDocument();
            doc.LoadHtml(subhtml);

            var res = doc.DocumentNode.SelectSingleNode("//table");
            if (res != null)
            {
                var list2 = res.SelectNodes("//td[@class='td_02']/div/p/a");//选择标签数组
                var list3 = res.SelectNodes("//td[@class='td_03']/p/span");//选择标签数组
                if (list2.Count > 0 && list2.Count == list3.Count)
                {
                    for (int i = 0; i < list2.Count; i++)
                    {
                        TopNewItem item = new TopNewItem();

                        item.Title = list2[i].InnerText;
                        item.Url = "http://s.weibo.com" + list2[i].Attributes["href"].Value;
                        item.Describe = list3[i].InnerText;

                        rs.Add(item);
                    }
                }
            }

            return rs;
        }
    }
}
